package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.GunsApplication;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.DoctorInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.service.IDoctorLookedService;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.ConsultantInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.TcRevisitInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 微信开发者接入
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/doctorPatientsCentral")
public class DoctorPatientsCentralController extends BaseController {
    private String PREFIX = "/weixin/app/";
    @Autowired
    private RedisTemplate redisTemplate;
    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    IDoctorLookedService doctorService;
    @Autowired
    ICustomerService customerService;

    /**
     * 医生患者列表加载
     */
    @RequestMapping(value = "/getDoctorPatient", method = RequestMethod.POST)
    @ResponseBody
    public List<DoctorLookedInfo> getTcPatient(String reach, String startTime) {
        SearchDoctorLookedInfo info = new SearchDoctorLookedInfo();
        Page<DoctorLookedInfo> page = new Page<>();
        info.setStartIndex(0);
        info.setLimitIndex(50);
        info.setReach(reach);
        info.setStartTime(startTime);
        page.setCurrent(0);
        page.setSize(50);
        List<DoctorLookedInfo> list = doctorService.selectPageListByQueryGetMap(page, info);
        return list;
    }

    /**
     * APP - 医生
     * 点击患者跳转页面 - 详情
     */
    @RequestMapping("/getDoctorPatient_details/{customerId}")
    public String getTcPatient_details(@PathVariable String customerId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql = "select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ," +
                "a.* from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " a where id ='" + customerId + "'";
        ReservationsInfo info = sqlMapper.selectOne(sql, ReservationsInfo.class);
        model.addAttribute("info", info);
        return PREFIX + "doctor/patient/patient_details.html";
    }

    /**
     * APP - 医生
     * 点击回访跳转页面 - 回访详情
     */
    @RequestMapping("/doctorPatient_returnVisit/{reservationsId}")
    public String patient_returnVisit(@PathVariable String reservationsId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //随访记录
        String sql4 = "select r.id,r.reservations_id,r.customer_id,r.revisit,r.revisit_time,r.outpatient_name,r.revisit_content,r.doctor,r.revisit_status," +
                "r.remarks  from return_doctor_visit_info r where r.reservations_id ='" + reservationsId + "' ";
        List<DoctorRevisitInfo> doctorRevisitInfoList = sqlMapper.selectList(sql4, DoctorRevisitInfo.class);
        model.addAttribute("doctorRevisitInfoList", doctorRevisitInfoList);
        model.addAttribute("reservationsId", reservationsId);
        return PREFIX + "doctor/patient/patient_returnVisit.html";
    }


    /**
     * APP - 医生
     * 点击回访跳转页面 - 回访详情
     */
    @RequestMapping("/doctorPatient_returnVisitTwo/{reservationsId}")
    public String doctorPatient_returnVisitTwo(@PathVariable String reservationsId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //随访记录
        String sql4 = "select r.id,r.reservations_id,r.customer_id,r.revisit,r.revisit_time,r.outpatient_name,r.revisit_content,r.doctor,r.revisit_status," +
                "r.remarks  from return_doctor_visit_info r where r.reservations_id ='" + reservationsId + "' ";
        List<DoctorRevisitInfo> doctorRevisitInfoList = sqlMapper.selectList(sql4, DoctorRevisitInfo.class);
        model.addAttribute("doctorRevisitInfoList", doctorRevisitInfoList);
        return PREFIX + "doctor/patient/patient_returnVisit_Two.html";
    }


    /**
     * APP - 医生
     * 病例记录
     */
    @RequestMapping("/doctor_caseList/{reservationsId}")
    public String doctor_caseList(@PathVariable String reservationsId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //病例记录
        //1、先查询当前客户的所有病例记录
        String caseInfoSql = "select left(t1.finnally_time,19) as finnallyTime1,t1.* from case_info t1 " +
                " where t1.reservations_id='" + reservationsId + "'order by t1.finnally_time desc";
        List<CaseInfo> caseInfoList = sqlMapper.selectList(caseInfoSql, CaseInfo.class);
        for (CaseInfo infos : caseInfoList) {
            if (infos != null) {
                if (infos.getOperationTime() != "" && infos.getOperationTime() != null) {
                    infos.setOperationTime(infos.getOperationTime().substring(0, 10));
                }
                String sql3 = "select pay_service_name as payServiceName from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "pay_service_info", 0) + " where id ='" + infos.getPayServiceId() + "' limit 1";
                CaseInfo caseInfo1 = sqlMapper.selectOne(sql3, CaseInfo.class);
                if (caseInfo1 == null) {
                    continue;
                }
                infos.setPayServiceName(caseInfo1.getPayServiceName());
            }
        }
        //2.循环遍历每个病例记录,给对应病例的牙位属性赋值
        for (CaseInfo caseInfo : caseInfoList) {
            //然后查询牙位集合
            String toothSIteSql = "select * from tooth_site t1 where t1.case_id='" + caseInfo.getId() + "' ";
            List<ToothSite> toothSiteList = sqlMapper.selectList(toothSIteSql, ToothSite.class);
            for (ToothSite toothSite : toothSiteList) {
                //根据对应的牙位类型,给病例实体的对应牙位属性赋值
                if (toothSite.getYaType().equals("1")) {
                    caseInfo.getToothSite1List().add(toothSite);
                }
                if (toothSite.getYaType().equals("2")) {
                    caseInfo.getToothSite2List().add(toothSite);
                }
                if (toothSite.getYaType().equals("3")) {
                    caseInfo.getToothSite3List().add(toothSite);
                }
                if (toothSite.getYaType().equals("4")) {
                    caseInfo.getToothSite4List().add(toothSite);
                }
                if (toothSite.getYaType().equals("5")) {
                    caseInfo.getToothSite5List().add(toothSite);
                }
            }
        }
        CustomerInfo customerInfo = customerService.selectOneByReservationsId(reservationsId);
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("caseInfoList", caseInfoList);
        return PREFIX + "doctor/case/doctor_caseList.html";
    }


    /**
     * APP - 医生
     * 病例记录
     */
    @RequestMapping("/doctor_caseList_Two/{reservationsId}")
    public String doctor_caseList_Two(@PathVariable String reservationsId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //病例记录
        //1、先查询当前客户的所有病例记录
        String caseInfoSql = "select left(t1.finnally_time,19) as finnallyTime1,t1.* from case_info t1 " +
                " where t1.reservations_id='" + reservationsId + "'order by t1.finnally_time desc";
        List<CaseInfo> caseInfoList = sqlMapper.selectList(caseInfoSql, CaseInfo.class);
        for (CaseInfo infos : caseInfoList) {
            if (infos != null) {
                if (infos.getOperationTime() != "" && infos.getOperationTime() != null) {
                    infos.setOperationTime(infos.getOperationTime().substring(0, 10));
                }
                String sql3 = "select pay_service_name as payServiceName from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "pay_service_info", 0) + " where id ='" + infos.getPayServiceId() + "' limit 1";
                CaseInfo caseInfo1 = sqlMapper.selectOne(sql3, CaseInfo.class);
                if (caseInfo1 == null) {
                    continue;
                }
                infos.setPayServiceName(caseInfo1.getPayServiceName());
            }
        }
        //2.循环遍历每个病例记录,给对应病例的牙位属性赋值
        for (CaseInfo caseInfo : caseInfoList) {
            //然后查询牙位集合
            String toothSIteSql = "select * from tooth_site t1 where t1.case_id='" + caseInfo.getId() + "' ";
            List<ToothSite> toothSiteList = sqlMapper.selectList(toothSIteSql, ToothSite.class);
            for (ToothSite toothSite : toothSiteList) {
                //根据对应的牙位类型,给病例实体的对应牙位属性赋值
                if (toothSite.getYaType().equals("1")) {
                    caseInfo.getToothSite1List().add(toothSite);
                }
                if (toothSite.getYaType().equals("2")) {
                    caseInfo.getToothSite2List().add(toothSite);
                }
                if (toothSite.getYaType().equals("3")) {
                    caseInfo.getToothSite3List().add(toothSite);
                }
                if (toothSite.getYaType().equals("4")) {
                    caseInfo.getToothSite4List().add(toothSite);
                }
                if (toothSite.getYaType().equals("5")) {
                    caseInfo.getToothSite5List().add(toothSite);
                }
            }
        }
        CustomerInfo customerInfo = customerService.selectOneByReservationsId(reservationsId);
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("caseInfoList", caseInfoList);
        return PREFIX + "doctor/case/doctor_caseList_Two.html";
    }


    /**
     * APP - 医生
     * 跳转到病历添加页面
     */
    @RequestMapping("/doctor_caseAdd/{customerId}")
    public String doctor_caseAdd(@PathVariable String customerId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0) == null) {
            String doctorListSql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "" + "' and (c.name='医生' or c.name='洁牙师') and a.status='1';";
            List<DoctorInfo> doctorInfoList = sqlMapper.selectList(doctorListSql, DoctorInfo.class);
            redisTemplate.opsForList().leftPush(ShiroKit.getUser().getAccount() + "DoctorInfoList", doctorInfoList);
        }
        model.addAttribute("doctorList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0));
        if (redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "nursesList", 0) == null) {
            String getListSql = " select a.id as id,a.name as nurse from user a left join dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where a.status='1' and (b.abbreviation='" + tableHead + "' and c.name in ('护士','护士长') and a.`status`='1')";
            redisTemplate.opsForList().leftPush(ShiroKit.getUser().getAccount() + "nursesList", sqlMapper.selectList(getListSql));
        }
        String selectsql = "select * from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " where id='" + customerId + "' limit 1";
        CustomerInfo customerInfo = sqlMapper.selectOne(selectsql, CustomerInfo.class);
        if (customerInfo != null) {
            model.addAttribute("reservationsId", customerInfo.getReservationsId());
        }
        String sql2 = "select p.pay_service_name as payServiceName,t.pay_service_id as id from  " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "deal_info", 0) + "  t " +
                "left join " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "pay_service_info", 0) + " p on p.id=t.pay_service_id " +
                "where reservations_id = '" + customerInfo.getReservationsId() + "' group by p.pay_service_name";
        List<com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo> payServiceInfo = sqlMapper.selectList(sql2, PayServicedInfo.class);
        //获得患者的到店时间
        String sql1 = "select left(arrival_time,19) as arrivalTime from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " " +
                "where arrival_status!=1 and arrival_status!=6 and arrival_status!=7 and arrival_status!=8 and arrival_status!=9 and arrival_status!=10 and reservations_id = '" + customerInfo.getReservationsId() + "' and arrival_time is not null order by arrival_time DESC ";
        List<CustomerInfo> customerList = sqlMapper.selectList(sql1, CustomerInfo.class);
        //患者信息
        String sqlSelect = " select " +
                "  (CASE a.customer_sex " +
                "  WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                " ) as sex ,a.* from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " a where id='" + customerId + "' limit 1";
        ConsultantInfo consultantInfo = sqlMapper.selectOne(sqlSelect, ConsultantInfo.class);
        model.addAttribute("nursesList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "nursesList", 0));
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("customerList", customerList);
        model.addAttribute("customerId", customerId);
        model.addAttribute("reservationsId", customerInfo.getReservationsId());
        model.addAttribute("payServiceInfo", payServiceInfo);
        model.addAttribute("consultantInfo", consultantInfo);
        return PREFIX + "doctor/case/doctor_caseAdd.html";
    }


    //加载医生随访列表
    @RequestMapping(value = "/doctor_returnVisit_list", method = RequestMethod.POST)
    @ResponseBody
    public List<DoctorLookedInfo> doctor_returnVisit_list(String startRevisitTime, String endRevisitTime, Integer revisitStatus, String revisit, HttpServletRequest request) {
        SearchDoctorLookedInfo info = new SearchDoctorLookedInfo();
        Page<DoctorLookedInfo> page = new Page<>();
        info.setStartIndex(0);
        info.setLimitIndex(50);
        info.setRevisitStatus(revisitStatus);
        info.setStartRevisitTime(startRevisitTime);
        info.setEndRevisitTime(endRevisitTime);
        info.setRevisit(revisit);
        page.setCurrent(0);
        page.setSize(50);
        List<DoctorLookedInfo> list = doctorService.selectPageListByDoctorRevisit(page, info);
        return list;
    }

    //跳转到添加回访页面--医生
    @RequestMapping("/doctor_patient_addVisit/{reservationsId}")
    public String doctor_patient_addVisit(@PathVariable String reservationsId, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0) == null) {
            String doctorListSql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "" + "' and (c.name='医生' or c.name='洁牙师') and a.status='1';";
            List<DoctorInfo> doctorInfoList = sqlMapper.selectList(doctorListSql, DoctorInfo.class);
            redisTemplate.opsForList().leftPush(ShiroKit.getUser().getAccount() + "DoctorInfoList", doctorInfoList);
        }
        model.addAttribute("doctorList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0));
        String sql = "select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ," +
                "a.* from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " a " +
                "where reservations_id ='" + reservationsId + "' order by arrival_time desc limit 1";
        CustomerInfo info = sqlMapper.selectOne(sql, CustomerInfo.class);
        model.addAttribute("info", info);
        model.addAttribute("reservationsId", reservationsId);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "doctor/patient/patient_addVisit.html";
    }


    /**
     * APP - tc
     * 点击回访跳转页面 - 回访详情 编辑
     */
    @RequestMapping("/doctor_patient_editVisit/{reservationsId}/{id}")
    public String doctor_patient_editVisit(@PathVariable String reservationsId, @PathVariable String id, Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0) == null) {
            String doctorListSql = " select a.id as id,a.account as doctor from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on find_in_set(c.id,a.roleid) > 0 " +
                    " where b.abbreviation='" + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "Abbreviation", 0) + "" + "' and (c.name='医生' or c.name='洁牙师') and a.status='1';";
            List<DoctorInfo> doctorInfoList = sqlMapper.selectList(doctorListSql, DoctorInfo.class);
            redisTemplate.opsForList().leftPush(ShiroKit.getUser().getAccount() + "DoctorInfoList", doctorInfoList);
        }
        model.addAttribute("doctorList", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "DoctorInfoList", 0));
        String sql = "select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ," +
                "a.* from " + redisTemplate.opsForList().index(ShiroKit.getUser().getAccount() + "customer_info", 0) + " a " +
                "where reservations_id ='" + reservationsId + "' order by arrival_time desc limit 1";
        CustomerInfo info = sqlMapper.selectOne(sql, CustomerInfo.class);
        String sql1 = "select * from return_doctor_visit_info where id ='" + id + "'";
        TcRevisitInfo doctorRevisit = sqlMapper.selectOne(sql1, TcRevisitInfo.class);
        model.addAttribute("doctorRevisit", doctorRevisit);
        model.addAttribute("info", info);
        model.addAttribute("reservationsId", reservationsId);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "doctor/patient/patient_editVisit.html";
    }


    /**
     * 修改  随访保存待计划
     */
    @RequestMapping(value = "/editDocotrVisit1", method = RequestMethod.POST)
    @ResponseBody
    public int editDocotrVisit1(String doctor, String createName,
                                String revisitTime, String revisitContent, String id,
                                String remarks, String customerId, String reservationsId, String revisit) {
        String sql = "update return_doctor_visit_info set doctor='" + doctor + "',create_name='" + createName
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent
                + "',remarks='" + remarks + "',revisit='" + revisit + "',revisit_status='1' " +
                "where reservations_id='" + reservationsId + "' and id='" + id + "'";
        //插入随访日志
        String sq11 = "INSERT INTO `doctor_visit_info_log` (createtime,single_person,logtype, `userid`," +
                " `succeed`,outpatient_name ,`message`,customer_id,reservations_id) VALUES " +
                " ('" + DateTimeKit.now() + "','" + ShiroKit.getUser().getAccount() + "','修改随访','" + ShiroKit.getUser().getId() + "','成功','" + ShiroKit.getUser().getOutpatientName() +
                "','随访结果：" + revisitContent + "</br>\n随访状态：待计划->待计划</br>\n','" + customerId + "','" + reservationsId + "');";
        sqlMapper.insert(sq11);
        return sqlMapper.update(sql);
    }

    /**
     * 修改 完成随访
     */
    @RequestMapping(value = "/editDocotrVisit2", method = RequestMethod.POST)
    @ResponseBody
    public int editDocotrVisit2(String doctor, String createName,
                                String revisitTime, String revisitContent, String id,
                                String remarks, String reservationsId, String customerId, String revisit) {
        String sql = "update return_doctor_visit_info set doctor='" + doctor + "',create_name='" + createName
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent
                + "',remarks='" + remarks + "',revisit='" + revisit + "',revisit_status='2' " +
                "where reservations_id='" + reservationsId + "' and id='" + id + "'";
        //插入随访日志
        String sq11 = "INSERT INTO `doctor_visit_info_log` (createtime,single_person,logtype, `userid`," +
                " `succeed`,outpatient_name ,`message`,customer_id,reservations_id) VALUES " +
                " ('" + DateTimeKit.now() + "','" + ShiroKit.getUser().getAccount() + "','修改随访','" + ShiroKit.getUser().getId() + "','成功','" + ShiroKit.getUser().getOutpatientName() +
                "','随访结果：" + revisitContent + "</br>\n随访状态：待计划->回访完成</br>\n','" + customerId + "','" + reservationsId + "');";
        sqlMapper.insert(sq11);
        return sqlMapper.update(sql);
    }

    /**
     * 到店的微信公众号绑定
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchCustomerInfo info, HttpServletRequest request) {
        Page<CustomerInfo> page = new PageFactory<CustomerInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<CustomerInfo> list = customerService.selectPageListById(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = customerService.selectPageListById(page, info);
            }
            page.setTotal(customerService.getQueryPagesByIdCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}